﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Autofac;
using Autofac.Integration.Web.Mvc;
using MovieDB.Business;
using MovieDB.Common.Exceptions;
using MovieDB.Data.Repositories;
using NHibernate;

namespace MovieDB.Web.Infrastructure
{
    public static class Components
    {
        public static void RegistrationComponents(ContainerBuilder builder)
        {
            var webAssembly = typeof (Components).Assembly;
            var dataAssembly = typeof (IRepository<>).Assembly;
            var commonAssembly = typeof (ExceptionHelper).Assembly;
            var businessAssembly = typeof (IReferenceDataManager).Assembly;

            builder.RegisterControllers(webAssembly);
            builder.RegisterAssemblyTypes(webAssembly, dataAssembly, commonAssembly, businessAssembly)
                .AsImplementedInterfaces()
                .InstancePerLifetimeScope();

            builder.RegisterGeneric(typeof (Repository<>))
                .As(typeof(IRepository<>))
                .InstancePerLifetimeScope();

            var sessionFactory = SessionProvider.SessionFactory;
            builder.RegisterInstance(sessionFactory)
                .As<ISessionFactory>()
                .SingleInstance();

            builder.Register(context => context.Resolve<ISessionFactory>().OpenSession())
                .As<ISession>()
                .InstancePerLifetimeScope();
        }
    }
}